package com.zixun.controller;

import com.zixun.config.WebApiConfig;
import com.zixun.model.ApplyInfo;
import com.zixun.model.User;
import com.zixun.model.information.Information;
import com.zixun.repository.ApplyInfoRepository;
import com.zixun.repository.UserRepository;
import com.zixun.repository.information.InformationRepository;
import com.zixun.service.InformationService;
import com.zixun.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Basil on 16/8/12.
 */
@RestController
@Transactional
@RequestMapping("/api/expert")
public class ExpertController {

    @Autowired
    UserRepository userRepository;

    @Autowired
    UserService userService;

    @Autowired
    InformationService informationService;

    @Autowired
    InformationRepository informationRepository;

    @Autowired
    ApplyInfoRepository applyInfoRepository;

    /**
     * @api {GET} /api/expert/getExpertList 获取行家列表
     * @apiName getExpertList
     * @apiGroup expert
     * @apiVersion 0.0.1
     * @apiDescription 获取行家列表
     * @apiParam {Integer} countPerPage 显示数量
     * @apiParam {Integer} pageIndex 当前页码
     * @apiSuccessExample 返回值: HTTP/1.1 200 OK
     * {
     * "returnValue":【1:成功, 2:参数错误,3:服务器异常, 整型】,
     * "errMsg":【错误信息, 字符串】
     * "data":[{
     * "userId":【行家编号, 字符串】,
     * "name":【行家名称, 字符串】,
     * "level":【行家评级, 整型】,
     * "city":【行家城市, 字符串】,
     * "number":【行家接单数量, 整型】,
     * "expertise":【行家专长, 字符串】,
     * "headPicture":【行家头像, 字符串】
     * }]
     * }
     */
    @RequestMapping("getExpertList")
    public Map<String, Object> getExpertList(Integer countPerPage, Integer pageIndex) {
        Map<String, Object> result = new HashMap();
        try {
            if (countPerPage == null || pageIndex == null) {
                result.put("errMsg", "参数异常");
                result.put("returnValue", 2);
                return result;
            }
            Page<User> userPage = userService.findAllByIsExpert(1, pageIndex, countPerPage);
            System.out.println("userPage+" + userPage);
            ArrayList list = new ArrayList();
            for (User user : userPage) {
                Map data = new HashMap();
                data.put("userId", user.getId().toString());
                data.put("name", user.getName());
                if(user.getLevel()==null){
                    user.setLevel(0);
                    userRepository.save(user);
                }
                data.put("level", user.getLevel());
                data.put("city", user.getCity());
                if(user.getExpertNumber()==null) {
                    user.setExpertNumber(0);
                    userRepository.save(user);
                }

                    data.put("number",user.getExpertNumber());
                ApplyInfo applyInfo =applyInfoRepository.findByUserId(user.getId());
                data.put("expertise", applyInfo.getExpertise());
                data.put("headPicture", WebApiConfig.IMAGE_URL+user.getHeadPicture());
                list.add(data);
            }
            result.put("errMsg", "成功");
            result.put("returnValue", 1);
            result.put("data", list);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.put("errMsg", "服务器异常");
            result.put("returnValue", 3);
            return result;
        }
    }

    /**
     * @api {GET} /api/expert/getExpertInfo 获取行家详情
     * @apiName getExpertInfo
     * @apiGroup expert
     * @apiVersion 0.0.1
     * @apiDescription 获取行家详情
     * @apiParam {String} userId 行家编号
     * @apiParam {Integer} informationId 资信编号
     * @apiSuccessExample 返回值: HTTP/1.1 200 OK
     * {
     * "returnValue":【1:成功, 2:参数错误,3:服务器异常,10:用户不存在 整型】,
     * "errMsg":【错误信息, 字符串】
     * "data":{
     * "realName":【行家名字, 字符串】,
     * "headPicture":【行家头像, 字符串】,
     * "jobCard":【工作名片, 字符串】,
     * "company":【单位名称, 字符串】,
     * "expertNumber":【行家接单数量, 整型】,
     * "expertise":【行家专长, 字符串】,
     * "service":【服务范围, 字符串】,
     * "starLevel":【星级, 整型】,
     * "price":【价格, 整型】,
     * "takeIdNumber":【手持身份证,字符串】,
     * "sex":【性别，整形】,
     * "mobilephone":【手机号码，字符串】,
     * "edu":【学历，整形】,
     * "address":【所在区域，字符串】,
     * "department":【部门，字符串】,
     * "post":【岗位，字符串】,
     * "companyTitle":【职称，字符串】,
     * "industryId":【行业，字符串】,
     * }
     */
    @RequestMapping("getExpertInfo")
    public Map getExpertInfo(String userId,Integer informationId){
        Map<String,Object> result = new HashMap<>();
        if(userId==null){
            result.put("errMsg","参数错误");
            result.put("returnValue",2);
            return result;
        }
        if (!userService.isUserExist(Integer.valueOf(userId))){
            result.put("errMsg","用户不存在");
            result.put("returnValue",10);
            return result;
        }
        User user = userService.getUserByUserId(Integer.valueOf(userId));
        Map<String,Object> map = new HashMap<>();
        map.put("headPicture", WebApiConfig.IMAGE_URL + user.getHeadPicture());
        map.put("expertNumber", user.getExpertNumber());
        if(user.getStarLevel()==null){
            user.setStarLevel(0);
            userRepository.save(user);
        }
        map.put("starLevel", user.getStarLevel());
        ApplyInfo applyInfo =applyInfoRepository.findByUserId(user.getId());
        map.put("jobCard",WebApiConfig.IMAGE_URL+applyInfo.getJobCard());
        map.put("name",applyInfo.getRealName());
        map.put("company",applyInfo.getCompany());
        map.put("service",applyInfo.getService());
        map.put("expertise",applyInfo.getExpertise());
        map.put("mobilephone",applyInfo.getMobilephone());
        map.put("companyTitle",applyInfo.getCompnayTitle());
        map.put("takeIdNumber",WebApiConfig.IMAGE_URL+applyInfo.getTakeIdNumber());
        map.put("address",applyInfo.getAddress());
        if(applyInfo.getIndustryId()==null){
            applyInfo.setIndustryId(0);
            applyInfoRepository.save(applyInfo);
        }
        map.put("industryId",applyInfo.getIndustryId());
        map.put("department",applyInfo.getDepartment());
        if(applyInfo.getSex()==null){
            applyInfo.setSex(0);
            applyInfoRepository.save(applyInfo);
        }
        map.put("sex", applyInfo.getSex());
        if(applyInfo.getEdu()==null){
            applyInfo.setEdu(0);
            applyInfoRepository.save(applyInfo);
        }
        map.put("edu",applyInfo.getEdu());
        map.put("post",applyInfo.getPost());
        Information information =informationService.findInformationByInformationId(informationId);
        if(information.getPrice()==null){
            information.setPrice(0);
            informationRepository.save(information);
        }
        map.put("price",information.getPrice());
        result.put("data",map);
        result.put("errMsg","成功");
        result.put("returnValue",1);
        return result;
    }

}
